<template>
  <div class="personal-main">
    <div class="pmain-profile">
      <div class="pmain-welcome">
        <span class="fr">上次登录时间： {{ userIndexVO.lastLoginTime }} </span>
      </div>
      <div class="pmain-user">
        <div class="user-head">
          <span class="head-img">
            <span>
              <img
                :src="userIndexVO.headImg"
                style="width: 88px; height: 88px; z-index: 0"
              />
              <i class="headframe" style="z-index: 0"></i>
            </span>
          </span>
        </div>
        <div class="user-info">
          <ul>
            <li>
              用户名<span>{{ userIndexVO.name }}</span>
              <NuxtLink
                v-if="
                  userIndexVO.userType === 2 && userIndexVO.bindStatus === 1
                "
                to="/user/borrower"
              >
                立即借款
              </NuxtLink>
            </li>
            <li v-if="userIndexVO.bindStatus !== 1">
              您还未开通第三方支付账户，请
              <NuxtLink to="/user/bind">立即开通</NuxtLink>
              以确保您的正常使用和资金安全。
            </li>
          </ul>
        </div>
      </div>

      <div v-if="userIndexVO.bindStatus === 1" class="pmain-money">
        <ul>
          <li class="none">
            <span>
              <em>账户余额</em>
              <i class="markicon"></i>
            </span>
            <span class="truemoney">
              <i class="f26 fb">{{ userIndexVO.amount }}</i> 元
            </span>
          </li>
          <li>
            <span>
              <em>冻结金额</em>
              <i class="markicon"></i>
            </span>
            <span class="truemoney">
              <i class="f26 fb">{{ userIndexVO.freezeAmount }}</i
              >元
            </span>
          </li>
          <li>
            <NuxtLink to="/user/recharge">
              <el-button size="mini" type="danger">充值</el-button>
            </NuxtLink>
            <NuxtLink to="/user/withdraw">
              <el-button size="mini" type="success">提现</el-button>
            </NuxtLink>
          </li>
        </ul>
      </div>
    </div>

    <el-dialog
      title="请绑定手机号"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
    >
      <el-form :model="form">
        <el-form-item label="手机号码" :label-width="formLabelWidth">
          <el-input v-model="bindMobileVO.mobile" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户类型" :label-width="formLabelWidth">
          <el-select
            v-model="bindMobileVO.userType"
            placeholder="请选择活动区域"
          >
            <el-option label="借款人" :value="2"></el-option>
            <el-option label="投资人" :value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onBindMobileBtn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userIndexVO: {},
      dialogFormVisible: false,
      form: {},
      formLabelWidth: "130px",
      bindMobileVO: {
        userType: 2,
      },
    };
  },

  mounted() {
    this.isUserBindMobile();
    this.fetchUserData();
  },

  methods: {
    onBindMobileBtn() {
      this.bindMobile();
    },
    //  获取用户个人中心首页的数据
    fetchUserData() {
        let token = localStorage.getItem("SRB-TOKEN")
        if(!token){
          this.$message.error("用户信息已经过期")
          return ;
        }
        this.$axios({
          method: 'GET',
          url: '/api/core/userInfo/index',
          headers: {token}
        }).then(r=>{
          this.userIndexVO = r.data.data.item
        })
    },
    isUserBindMobile() {
      let token = localStorage.getItem("SRB-TOKEN");
      if (!token) {
        this.$message.error("登陆过期!");
        this.$router.push("/login");
        return;
      }
      this.$axios({
        method: "GET",
        url: "/api/core/userInfo/isBindMobile",
        headers: {
          token,
        },
      }).then((r) => {
        this.dialogFormVisible = !r.data.data.flag;
      });
    },
    bindMobile() {
      let token = localStorage.getItem("SRB-TOKEN");
      if (!token) {
        this.$message.error("登陆过期!");
        this.$router.push("/login");
        return;
      }
      this.$axios({
        method: "PUT",
        url: "/api/core/userInfo/mobileAndType",
        data: this.bindMobileVO,
        headers: { token },
      }).then((r) => {
        this.$message.success(r.data.message);
        
        window.location.href = "/user";
      });
    },
  },
};
</script>
